package com.exam.controller;

import com.exam.pojo.entity.Department;
import com.exam.pojo.entity.Student;
import com.exam.pojo.mode.DepartVo;
import com.exam.pojo.mode.DepartmentVo;
import com.exam.pojo.mode.StudentVo;
import com.exam.pojo.params.PageParam;
import com.exam.service.DepartmentService;
import com.exam.utils.PageUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class DepartmentController {

    @Resource
    private DepartmentService departmentService;

    /**
     * 部门列表
     * @param model
     * @return
     */
    @RequestMapping("user-add.html")
    public String findAllList(Model model){
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);
        return "user-add";
    }

    /**
     * 部门列表
     * @param model
     * @return
     */
    @RequestMapping("department-add.html")
    public String findAlldepartmentList(Model model){
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);
        return "department-add";
    }

    /**
     * 分页 部门管理
     */
    @RequestMapping("department.html")
    public String listDepartVo(Integer currentPage, Model model){
        //统计总数
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        int total = departmentVoList.size();

        currentPage = PageUtils.getCurrentPage(currentPage,total);

        //根据当前页查询当前页的数据
        List<DepartVo> listDepartVo = departmentService.listDepartVo((currentPage - 1) * 5, 5);
        model.addAttribute("listDepartVo",listDepartVo);

        //将当前页、总数、每页数据个数（5：后期可以自定义）给前端
        PageParam pageParam = new PageParam(total,currentPage,5);
        model.addAttribute("pageParam",pageParam);
        return "department";
    }

    /**
     * 添加部门
     * @param department
     */
    @PostMapping("addDepartment")
    @ResponseBody
    public void addDepartment(@RequestBody Department department){
        departmentService.addDepartment(department);
    }

    /**
     * 删除部门
     * @param departmentId
     * @return
     */
    @PostMapping("delDepartment/{departmentId}")
    @ResponseBody
    public void delDepartment(@PathVariable("departmentId") Integer departmentId){
        departmentService.delDepartment(departmentId);
//        return "department";
    }

    /**
     * 批量删除部门
     * @param departmentIdList
     * @return
     */
    @PostMapping("delAllDepartment")
    @ResponseBody
    public void delAllDepartment(@RequestBody List<Integer> departmentIdList){
        System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆"+departmentIdList);
        departmentService.delAllDepartment(departmentIdList);
//        return "department";
    }


    /**
     * 跳转部门编辑页
     * @param departmentId
     * @param model
     * @return
     */
    @RequestMapping("/department/toEditPage/{departmentId}")
    public String toEditPage(@PathVariable("departmentId") Integer departmentId, Model model){
        List<DepartmentVo> departmentVoList = departmentService.findAllList();
        model.addAttribute("departmentVoList",departmentVoList);
        model.addAttribute("departmentId",departmentId);
        return "department-edit";
    }

    /**
     * 更新部门信息
     * @param department
     */
    @PostMapping("editDepartment")
    @ResponseBody
    public void editStudent(@RequestBody Department department){
        departmentService.editDepartment(department);
    }

    /**
     * 搜索
     * @return
     */
    @PostMapping("findDepartmentByName")
    public String findDepartmentByName(String name,Model model){
        List<DepartVo> listDepartVo = departmentService.findDepartmentByName(name);
        int total = listDepartVo.size();
        model.addAttribute("listDepartVo",listDepartVo);

        //将当前页、总数、每页数据个数（5：后期可以自定义）给前端
        PageParam pageParam = new PageParam(total,1,5);
        model.addAttribute("pageParam",pageParam);

        return "department";
    }




}
